package cn.piesat.scanning.business.xz.service.drtimpl;

import cn.hutool.core.date.DateUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildXIZANG_SPI_MCIServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //SURF_CHN_MUL_DAY_20220101000000.json
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String[] split = fileName.split("_");
        String endTime = split[4].replace(".json", "");
        //根据endTime字符串，往前推160天，得出startTime
        Date date = DateUtil.parse(endTime, "yyyyMMddHHmmss");
        Date dateTime = DateUtil.offsetDay(date, -159);
        String startTime = DateUtil.format(dateTime, "yyyyMMddHHmmss");
        String productMode = split[2];
        String productIid = split[0];
        String region = split[1];
        String level = split[3];
        List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findByMetadata(startTime, endTime, level, "FY3D", productIid, region, productMode, "JSON");
        if (metadataDTOList.size() < 160) {
            algorithmParams.put("parameter01", null);
        } else {
            //将文件名生成集合
            List<String> collect = metadataDTOList.stream().map(x -> scanningPath + x.getFilePath()).collect(Collectors.toList());
            Map<String, Object> params = new HashMap<>();
            params.put("primaryFile", collect);
            params.put("auxPath", taskPathParam.getParameter02());
            algorithmParams.put("parameter01", params);
        }
        algorithmParams.put("parameter00", adsType);
        return algorithmParams;
    }
}
